How to Add Image Watermark in PDF Document
Watermarks can help secure different documents like PDF. Text watermark is directly and easy to add. Image watermark can give readers nice impressions. Fortunately, the watermark feature in PDF does not limit to Text. Users can use this feature to add images such as a family crest or company logo to the document. And Spire.PDF, enables developers to use C# add both text watermarks and image watermarks. Here we will discuss how to use C# add Image watermarks in PDF document. (Click to consult how to se C# add text watermark)
Tools: Visual Studio, Spire.PDF, basic programming knowledge
Main Tool Introduction:
Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat. It does not use any external libraries and is built from scratch in C#.
Friendly Reminder: Please make sure Spire.PDF and Visual Studio are correctly installed on your system.
Instructions:
Step 1 Create a Project
Create a C# project in Visual Studio and Add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using System;
using Spire.Pdf;
namespace Imagewatermark
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Step 2 Create PDF Document
Spire.PDF enables to create PDF documents with customized content. We can create a PDF document and set margins for later using on watermarks.
//Create a pdf document.
PdfDocument doc = new PdfDocument();
//margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
Step 3 C# Add Image Watermark in PDF
After created a PDF document, create a PDF page and then add an image to be background image and this is the image watermark.
// Create one page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
Image img = Image.FromFile(@"Background.png");
page.BackgroundImage = img;
Step 4 Save and Preview
The code below can help us save the file as .pdf format and preview after start the project.
//Save pdf file.
doc.SaveToFile("ImageWaterMark.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("ImageWaterMark.pdf");
Now, press F5 to start the project and an PDF document will be automatically generated.
Effective Screenshot:
More about Spire.PDF
Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat. Click to learn more…
Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat. Click to learn more…